package event;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Poker extends JFrame implements ActionListener, Runnable {
    public String style[] = {"红心", "方片", "黑桃", "梅花"};
    public String number[] = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
    List<Integer> list = new ArrayList<>();
    public String cardbuffer[] = new String[54];
    JButton button_start, button_interrupt, button_terminate;
    JTextField text_state;
    JLabel lable;
    Thread thread;
    JTextArea j1;
    JTextArea j2;
    JTextArea j3;
    JTextArea j4;
    JTextArea j5;
    JTextArea j6;
    JTextArea j7;
    JTextArea j8;
    JTextArea j9;

    public Poker() {
        super("扑克发牌");
        this.setSize(500, 400);
        this.setLocation(200, 300);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.getContentPane().setLayout(new BorderLayout());
        JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(3, 3, 10, 10));
        j1 = new JTextArea();
        j2 = new JTextArea();
        j3 = new JTextArea();
        j4 = new JTextArea();
        j5 = new JTextArea();
        j6 = new JTextArea();
        j7 = new JTextArea();
        j8 = new JTextArea();
        j9 = new JTextArea();
        j1.setEditable(false);
        j3.setEditable(false);
        j5.setEditable(false);
        j7.setEditable(false);
        j9.setEditable(false);
        j2.setFont(new Font("宋体", Font.PLAIN, 15));
        j4.setFont(new Font("宋体", Font.PLAIN, 15));
        j6.setFont(new Font("宋体", Font.PLAIN, 15));
        j8.setFont(new Font("宋体", Font.PLAIN, 15));
        j2.setLineWrap(true);// 激活自动换行功能
        j2.setWrapStyleWord(true);// 激活断行不断字功能
        j4.setLineWrap(true);// 激活自动换行功能
        j4.setWrapStyleWord(true);// 激活断行不断字功能
        j6.setLineWrap(true);// 激活自动换行功能
        j6.setWrapStyleWord(true);// 激活断行不断字功能
        j8.setLineWrap(true);// 激活自动换行功能
        j8.setWrapStyleWord(true);// 激活断行不断字功能
        p1.add(j1);
        p1.add(j2);
        p1.add(j3);
        p1.add(j4);
        p1.add(j5);
        p1.add(j6);
        p1.add(j7);
        p1.add(j8);
        p1.add(j9);
        this.getContentPane().add(p1);
        thread = new Thread(this);
        JPanel p = new JPanel();
        button_start = new JButton("启动");
        button_start.addActionListener(this);
        button_interrupt = new JButton("中断");
        button_interrupt.addActionListener(this);
        button_terminate = new JButton("线程结束");
        button_terminate.addActionListener(this);
        button_interrupt.setEnabled(false);
        button_terminate.setEnabled(false);
        thread = new Thread(this);
        lable = new JLabel("线程状态:");
        text_state = new JTextField(" " + thread.getState(), 10);
        text_state.setEditable(false);
        p.setBackground(Color.GRAY);
        p.setLayout(new FlowLayout(0));
        p.add(button_start);
        p.add(button_interrupt);
        p.add(button_terminate);
        p.add(lable);
        p.add(text_state);
        this.getContentPane().add(p, "South");
        this.setVisible(true);
        int ti = 0;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 13; j++) {
                cardbuffer[ti] = (style[i] + number[j] + " ");
                ti++;
            }
            cardbuffer[52] = "大王 ";
            cardbuffer[53] = "小王 ";
        }
        //将54个数字放入一个List中
        for (int i = 0; i < 54; i++) {
            list.add(i);
        }
        Collections.shuffle(list);
    }

    public void run() {
        for (int i = 0; i < 54; i++) {
            j2.append(cardbuffer[list.get(i)]);
            i++;
            try {
                Thread.sleep(300);//在此处设置速度
            } catch (InterruptedException e) {
                break;
            }
            j6.append(cardbuffer[list.get(i)]);
            i++;
            try {
                Thread.sleep(300);//在此处设置速度
            } catch (InterruptedException e) {
                break;
            }
            if (i >= 54) {
                break;
            }
            j8.append(cardbuffer[list.get(i)]);
            i++;
            try {
                Thread.sleep(300);//在此处设置速度
            } catch (InterruptedException e) {
                break;
            }
            j4.append(cardbuffer[list.get(i)]);

            try {
                Thread.sleep(300);//在此处设置速度
            } catch (InterruptedException e) {
                break;
            }
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == button_start) {
            thread = new Thread(this);
            thread.start();
            text_state.setText(" " + thread.getState());
            button_start.setEnabled(false);
            button_interrupt.setEnabled(true);
        }
        if (e.getSource() == button_interrupt) {
            thread.interrupt();
            text_state.setText(" " + thread.getState());
            button_interrupt.setEnabled(false);
            button_start.setEnabled(true);
            button_terminate.setEnabled(true);
        }
        if (e.getSource() == button_terminate) {
            text_state.setText(" " + thread.getState());
        }
    }

    public static void main(String[] args) {
        new Poker();
    }

}
